package dfbz.com.service.impl;

import dfbz.com.dao.UserDao;
import dfbz.com.model.Dept;
import dfbz.com.model.UserFocus;
import dfbz.com.model.toolmodel.PageBean;
import dfbz.com.model.User;
import dfbz.com.model.UserInfo;
import dfbz.com.service.UserService;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 在 service 层做业务逻辑处理
 * <p>
 * dao 层只做数据库的查询
 */

public class UserServiceImp implements UserService {
    //userDao与数据库关联，通过username找到对应用户并得到该用户的相关信息——包括密码
    private UserDao userDao = new UserDao();


    @Override
    public User findUser(String username) {

        return userDao.findUserByName(username);

    }

    @Override
    public User findUserByEmail(String email) {
        return userDao.findUserByEmail(email);
    }

    @Override
    public void add(Object o) {
        userDao.add(o);
    }


    @Override
    public void replacePw(User user) {
        userDao.update(user);
    }

    @Override
    public List showUser() {
        List list = userDao.addAll(UserInfo.class);
        return list;
    }

    @Override
    public User findUserById(Object id) {
        User user = (User) userDao.get(id, User.class);
        return user;
    }

    @Override
    public PageBean showUserByPage(String current, String row) {
        return null;
    }




    @Override
    public List<UserFocus> findFocusUser(Object userId) {
        List<UserFocus> userFocusList = userDao.findUserFocus(userId);

        return userFocusList;
    }

    @Override
    public List<UserFocus> findFocusUserPage(Object userId, Integer current) {
        List<UserFocus> userFocus = userDao.findUserFocus(userId, current);
        return userFocus;
    }




    @Override
    public List<Map<String, Object>> showUserByPage(Integer userId, String current, String search) {
        List<Map<String, Object>> list = userDao.findPageUser(userId, Integer.parseInt(current), search);
        return list;
    }

    @Override
    public long findTotalCount(String search) {
        long total = userDao.findListTotalCount(search);
        return total;
    }

    @Override
    public Dept findDeptName(Integer deptId) {
        Dept dept = (Dept) userDao.get(deptId, Dept.class);
        return dept;
    }

    @Override
    public Boolean updateLoginTime(Integer id) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(id);
        userInfo.setLoginTime(new Date());
        userDao.update(userInfo);
        return true;
    }

    @Override
    public UserFocus findFollowData(String userId, String uId) {

       UserFocus userFocus = userDao.findFollowData(userId,uId);
        return userFocus;
    }

    @Override
    public void addFollow(String userId, String uId) {
        UserFocus userFocus = new UserFocus();
        userFocus.setId(null);
        userFocus.setUserFocusId(Integer.parseInt(uId));
        userFocus.setUserId(Integer.parseInt(userId));
        userDao.addFocus(userFocus);
    }

    @Override
    public Long findFocusTotalCount(Object userId) {
        Integer ud = (Integer) userId;
      Long total =  userDao.findFocusTotalCount(ud);
        return total;
    }

    @Override
    public void deleteFollow(Object userId, String uId) {
        userDao.deleteFollow(userId.toString(),uId);
    }

    @Override
    public void updateImg(String path, String id) {
        userDao.updateImg(path,id);
    }



    @Override
    public UserInfo findUserInfoById(Integer id) {

       UserInfo userInfo = userDao.findUserInfo(id);
        return userInfo;
    }

    @Override
    public Map findUserDetailInfo(String userId) {

        Map<String, Object> userDetail = userDao.findUserDetail(userId);
        return userDetail;
    }

    @Override
    public Integer findDeptIdByDeptName(String dept) {
      Integer id = userDao.findDeptIdByDeptName(dept);
        return id;
    }

    @Override
    public void updateTable(Object table) {
        userDao.update(table);
    }

    @Override
    public void addLook(String uId) {
        userDao.addLook(uId);
    }

    @Override
    public Integer findFocusCount(String uId) {

      Integer count =  userDao.findFocusCount(uId);
        return count;
    }

    @Override
    public Integer findMyFocusCount(String userId) {

        return userDao.findMyFocusCount(userId);
    }


}
